<?php

namespace App\Http\Controllers\Demo;

use DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\table;
class HelloController extends Controller
{
    public function index(Request $request)
    {


        //创建数据
        if($request->isMethod('post'))
        {
            /*$resource=$request->all();*/
            $username=$request->input('username');
            $sex=$request->input('sex');
            $age=$request->input('age');

            $orm=Table::create(['username'=>$username,'sex'=>$sex,'age'=>$age]);

            if($orm) {

                return json_encode("success");
            }else{
                return json_encode("error");
            }
        }else{

            /*$t1O=DB::table('t1');
            $res=$t1O->get();
            $user=$t1O->paginate(5);*/
            //var_dump($user);
            #$data=['username'=>'haha','age'=>22];
            /*print_r($data);*/
            //显示及分页数据
            $user=Table::paginate(5);

            return view('demo.table',['user'=>$user]);
        }

    }

    public function dataOper(Request $request){


        if($request->isMethod('post')){
            $id=$request->input('id');
            $data=array(
                "username"=>$request->input('username'),
                "age"=>$request->input('age'),
                "sex"=>$request->input('sex')
            );
            if(Table::where("id",$id)->update($data)){
                return json_encode("success");
            }

        }else if($request->has('update')) {
            //更新显示数据
            $update_id=$request->input('update');
            $data=Table::find($update_id);
            return json_encode($data);
        }else if($request->has('delete')) {
            //删除数据
            $delete_id=$request->input('delete');
            if(Table::destroy($delete_id)){
                return "success";
            }
            return "hello Delete--$delete_id";
        }else{
            return "禁止访问";
        }
    }
}